const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const utils = require('../../../utils');

class SignupHandle extends BaseHandler {
    get pattern() {
        return "/signup";
    }

    doGET(request, response) {

        var session = request.session;
        if (session && session.user && session.user.authenticated) {
            response.redirect('/dashboard');
            return;
        }
        var security_code = utils.uuid();
        request.session.user.security_code = security_code;
        this.ensureRef(request);

        response.render('signup', {
            success: true,
            security_code: security_code,
            data: {}
        });
    }

    doPOST(request, response) {
        var me = this;
        var security_code = utils.uuid();

        if (!request.body.security_code || request.body.security_code !== request.session.user.security_code) {
            request.session.user.security_code = security_code;
            response.render('signup', {
                success: false,
                error: request.t('login.errors.invalid_request'),
                security_code: security_code,
                data: request.body
            });
            return;
        }
        var data = request.body;
        var name = (data.name || '').trim();
        var email = (data.email || '').trim();
        var password = (data.password || '').trim();
        var pageData = {
            is_login: false,
            data: data
        };
        pageData.security_code = security_code;
        request.session.user.security_code = security_code;
        var can_continue = true;
        if (!name || !email || !password) {
            can_continue = false;
            pageData.error = request.t('login.errors.user_or_email_or_password_empty');
        }

        if (can_continue && password.length < 6) {
            can_continue = false;
            pageData.error = request.t('login.errors.invalid_password_len');
        }

        if (can_continue && !utils.verifyEmailAddress(email)) {
            can_continue = false;
            pageData.error = request.t('login.errors.invalid_email_address');
        }
        pageData.success = can_continue;
        if (can_continue) {
            let locale = request.locale;
            this.Controller.saveOrUpdateSystemUser(name, email, password, request.session.user.refs, locale, null, (result) => {
                if (!result.success) {
                    pageData.success = false;
                    pageData.error = result.error;
                    response.render('signup', pageData);
                } else {
                    delete request.session.user.security_code;
                    request.session.user.authenticated = true;
                    request.session.user.id = result.data.id;
                    request.session.user.name = result.data.userName;
                    reques.session.user.nickName = result.data.nickName;
                    request.session.user.email = result.data.email;
                    request.session.user.registDatetime = result.data.registDatetime;
                    request.session.user.headImage = result.data.headImage || '';
                    request.session.user.storeId = result.data.storeId;
                    request.session.user.storeName = result.data.storeName;
                    request.session.user.role = result.data.role;
                    request.session.user.roleIds = result.data.roleIds;
                    response.redirect('/');
                }
            });
        } else {
            response.render('signup', pageData);
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = SignupHandle;